﻿Option Strict On
Imports USPSAddressValidation.Common

Namespace USPS
    <Serializable()> _
    Public Class AddressServices

        Public Function CreateXMLAddressRequest(address As USPS.Address) As String
            Dim retVal As New Text.StringBuilder
            With retVal
                .Append("<AddressValidateRequest USERID=")
                .Append("""").Append(UspsUserID)
                .Append(""">")
                '.Append("<Address ID=""0"">")
                .Append("<Address>")
                .Append("<Address1>")
                If address.Address1.Length > 0 Then
                    .Append(address.Address1)
                End If
                .Append("</Address1>")
                .Append("<Address2>")
                If address.Address2.Length > 0 Then
                    .Append(address.Address2)
                End If
                .Append("</Address2>")
                .Append("<City>")
                If address.City.Length > 0 Then
                    .Append(address.City)
                End If
                .Append("</City>")
                .Append("<State>")
                If address.State.Length > 0 Then
                    .Append(address.State)
                End If
                .Append("</State>")
                .Append("<Zip5>")
                If address.Zip5.Length > 0 Then
                    .Append(address.Zip5)
                End If
                .Append("</Zip5>")
                .Append("<Zip4>")
                If address.Zip4.Length > 0 Then
                    .Append(address.Zip4)
                End If
                .Append("</Zip4></Address></AddressValidateRequest>")
            End With
            Return retVal.ToString
        End Function

        ''' <summary>
        ''' Send request to remote site. Return reply data.
        ''' </summary>
        ''' <param name="USPS_Request">USPS_Request as String</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetDataFromSite(ByVal USPS_Request As String) As String
            Dim wsClient As New Net.WebClient
            Dim strResponse As String = String.Empty
            Dim ResponseData() As Byte

            Dim UrlRequest As String = BaseURL & "?API=Verify&XML=" & USPS_Request
            'Send the request to USPS.
            ResponseData = wsClient.DownloadData(UrlRequest)
            'Convert byte stream to string data.
            For Each oItem As Byte In ResponseData
                strResponse += Chr(oItem)
            Next oItem

            Return strResponse
        End Function

    End Class
End Namespace

